<?php
class Signup_Form extends TdPage {
	
	
	public $formsignup;
	
	function __construct($template) {
		parent::__construct("", $template);
		$this->doInit();
	}
	
	
	function doInit() {
		$this->createform();
	}
	
	
	function createform() {
	  	
	  	$formsignup = new TdForm("formsignup", "../core/app/form/form.phtml");
	  	$formsignup->setName("signup");
	  	$formsignup->setAction("signup.htm");
	  	
	  	
	  	$fieldset1 = new TdFieldset("fieldset_signup1", "../core/app/form/fieldset.phtml");
	  	$fieldset1->setName("fieldset_signup1");
	  	$formsignup->addField($fieldset1);
	  	
	  	
	  	$l_username = new TdLabel("label_username", "../core/app/form/label.phtml");
	  	$l_username->setClass("label_signup");
	  	$l_username->setValue(_("Username"));
	  	
	  	$username = new TdTextField("input_signup_username", "../core/app/form/textfield.phtml");
	  	$username->setLabel($l_username);
	  	$username->setRequired(true);
	  	$fieldset1->addField($username);
	  	
	  	
	  	$l_email = new TdLabel("label_email", "../core/app/form/label.phtml");
	  	$l_email->setClass("label_signup");
	  	$l_email->setValue(_("Email"));
	  	
	  	$email = new TdEmailField("input_signup_email", "../core/app/form/textfield.phtml");
	  	$email->setLabel($l_email);
	  	$email->setRequired(true);
	  	$fieldset1->addField($email);
	  	
	  	$fieldset2 = new TdFieldset("fieldset_signup2", "../core/app/form/fieldset.phtml");
	  	$fieldset2->setName("fieldset_signup2");
	  	$formsignup->addField($fieldset2);
	  	
	  	
	  	$l_password = new TdLabel("label_password", "../core/app/form/label.phtml");
	  	$l_password->setClass("label_signup");
	  	$l_password->setValue(_("Password"));
	  	
	  	$password = new TdPasswordField("input_signup_password", "../core/app/form/passwfield.phtml");
	  	$password->setLabel($l_password);
	  	$password->setRequired(true);
	  	$fieldset2->addField($password);
	  	
	  	
	  	$l_cpassword = new TdLabel("label_cpassword", "../core/app/form/label.phtml");
	  	$l_cpassword->setClass("label_signup");
	  	$l_cpassword->setValue(_("Confirm Password"));
	  	
	  	$password2 = new TdPasswordField("input_signup_confirm_password", "../core/app/form/passwfield.phtml");
	  	$password2->setLabel($l_cpassword);
	  	$password2->setRequired(true);
	  	$password2->setCompare($password);
	  	$fieldset2->addField($password2);
	  	 
	  	
	  	$submit = new TdSubmitField("submit_sign_up_1", "../core/app/form/submit.phtml");
	  	$submit->setValue("");
	  	$submit->setName("submit_sign_up_1");
	  	$submit->setClass("submit_sign_up_1");
	  	$formsignup->addField($submit);
	  	
	  	
	  	$this->addComponent($formsignup);
	  	
	  	$this->formsignup = $formsignup;
	  	
	  }
	  
	  
	  function doValidate() {
	  	global $teardrop;  	
	  	
	  	parent::doValidate();
	  	
	  	$utility = new cl_Utility();
	
	  	if (!$this->formsignup->gotError()) {
	  		$res = true;
	  		
	  		$newuser = new cl_User();
	  		$newuser->setEmail($this->formsignup->getField("fieldset_signup1")->getField("input_signup_email")->getValue());
	  		$newuser->setUsername($this->formsignup->getField("fieldset_signup1")->getField("input_signup_username")->getValue());
	  		$newuser->setPassword(md5($this->formsignup->getField("fieldset_signup2")->getField("input_signup_password")->getValue()));
	  		$res = $res && $newuser->create();

	  		$key = $utility->get_random_string("abcdefghijklmnopqrstuvwz0123456789", 32);
	  		$res = $res && $this->sql_con->insert("otp (otp.key, id_user) VALUES ('".$key."', ".$newuser->getId().");");

	  		$to = $this->formsignup->getField("fieldset_signup1")->getField("input_signup_email")->getValue();
	  		$to_name = $this->formsignup->getField("fieldset_signup1")->getField("input_signup_username")->getValue();

	  		$res = $res && $utility->send_welcome_email($key, $to, $to_name);

	  		if ($res) {
	      	$teardrop->route("welcome.htm", "username/".$newuser->getUsername());
	      }
	      else {
	      	trigger_error("Can't send welcome email to new subscriber.", E_USER_ERROR);
	      }
	  	}
	  }
	  	 
	
	
}
?>